package com.brillilab.service.core.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.crm.CrmReportFormDateMapper;
import com.brillilab.domain.po.crm.CrmReportFormDate;
import com.brillilab.service.core.crm.ICrmReportFormDateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author zhouxiong
 * @description
 * @date 2019/9/19 14:20
 */
@Service
@Transactional
public class CrmReportFormDateServiceImpl implements ICrmReportFormDateService {

    @Resource
    private CrmReportFormDateMapper crmReportFormDateMapper;

    @Override
    public CrmReportFormDate selectByTypeAndDate(Integer type, Date date) {
        LambdaQueryWrapper<CrmReportFormDate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmReportFormDate::getType, type);
        wrapper.apply("record_date = date({0})", date);
        List<CrmReportFormDate> formDates = crmReportFormDateMapper.selectList(wrapper);
        return CollectionUtils.isEmpty(formDates) ? null : formDates.get(0);
    }

    @Override
    public CrmReportFormDate insert(CrmReportFormDate reportFormDate) {
        Integer insert = crmReportFormDateMapper.insert(reportFormDate);
        Assert.isTrue(insert == 1, "操作失败！");
        return reportFormDate;
    }

    @Override
    public boolean update(CrmReportFormDate reportFormDate) {
        return crmReportFormDateMapper.updateById(reportFormDate) == 1;
    }

}
